# -*- coding: utf-8 -*-
"""
Created on Sat Apr 20 00:04:30 2025
Aim: Build User Profile
@author: DAI TIANRU
"""
import pandas as pd
import json
import os
import gc


#指定数据集路径
folder_path2 = 'D:/DTR/MyWork/2025/DataMining/personalProject/10G_data_new'
folder_path3 = 'D:/DTR/MyWork/2025/DataMining/personalProject/30G_data_new'

#读取数据集文件夹中.parquet数据文件
def read_parquet_folder(folder_path):
    all_data = []
    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        if filename.endswith('.parquet'):
            file_path = os.path.join(folder_path, filename)
            try:
                #1.建立用户画像需要使用的数据字段
                df = pd.read_parquet(file_path)
                all_data.append(df)
            except Exception as e:
                print(f"读取文件 {file_path} 时出错: {e}")

    # 合并所有数据
    combined_df = pd.concat(all_data, ignore_index=True)
    #垃圾回收多余的内存
    del all_data
    gc.collect()
    return combined_df

data = read_parquet_folder(folder_path2)
df = pd.DataFrame(data)
del data
gc.collect()
user_profiles = []

# 遍历 DataFrame 的每一行
for index, row in df.iterrows():
    # 解析数据并构建用户画像
    user_profile = {
        '基本信息': {
            '姓名': row['fullname'],
            '性别': row['gender'],
            '邮箱': row['email'],
            '电话': row['phone_number'],
            '国籍': row['country'],
            '地址': row['address']
        },
        '消费特征': {
            '收入': float(row['income']),
            '消费时间': json.loads(row['purchase_history'])['purchase_date'],
            '消费品类': json.loads(row['purchase_history'])['categories'],
            '支付方式': json.loads(row['purchase_history'])['payment_method'],
            '支付状态': json.loads(row['purchase_history'])['payment_status']
        },
        '行为特征': {
            '注册时间': row['registration_date'],
            '登录信息': {
                '登录次数': json.loads(row['login_history'])['login_count'],
                '首次登录时间': json.loads(row['login_history'])['first_login'],
                '登录地点': json.loads(row['login_history'])['locations'],
                '使用设备': json.loads(row['login_history'])['devices'],
                '平均会话时长': json.loads(row['login_history'])['avg_session_duration'],
                '登录时间分布': json.loads(row['login_history'])['timestamps']
            }
        },
        '其他特征': {
            '会员状态': row['is_active'] == 'TRUE'
        }
    }
    user_profiles.append(user_profile)

# 打印所有用户画像
for i, user_profile in enumerate(user_profiles, start=1):
    print(f"用户 {i} 的画像信息:")
    for category, details in user_profile.items():
        print(f"  {category}:")
        for key, value in details.items():
            if isinstance(value, dict):
                print(f"    {key}:")
                for sub_key, sub_value in value.items():
                    print(f"      {sub_key}: {sub_value}")
            else:
                print(f"    {key}: {value}")
    print()